import * as API from "../../utils/betcenterUrl"
import * as AWARD from "../../utils/awardUrl"
import * as actionType from "../types/betcenter"
import {RES_OK_CODE} from "../types/index" 

import {post} from "../../utils/url"

export function loadPankKouList(gameCode) {
  return (dispatch,getState) =>{
    post(API.loadBetCenterAPI(),{
      gameCode
    })
    .then(res => res.json())
    .then(data => {
        dispatch({
          type: actionType.REQUEST_PANKOU_LIST,
          data
        })
        let itemCode = data.datas.defaultItemCode
        dispatch(loadPeilvData(gameCode,itemCode)) // 获取盘口
        dispatch(getGameInfo(gameCode,itemCode)) // 获取开盘
    });
  }
}

export function loadPeilvData(gameCode,itemCode) {
  return (dispatch,getState) =>{
    post(API.loadrefreshAPI(),{
      gameCode,
      itemCode
    })
    .then(res => res.json())
    .then(data => {
        dispatch({
          type: actionType.REQUEST_PEILV_DATA,
          data:{
            gameCode,
            itemCode,
            data
          }
        })
    });
  }
}

export function getGameInfo(gameCode,itemCode) {
  return (dispatch,getState) =>{
    post(API.getGameInfo(),{
      gameCode,
      itemCode
    })
    .then(res => res.json())
    .then(data => {
        if(data.errorCode == RES_OK_CODE){
            dispatch({
              type: actionType.REQUEST_GAME_INFO,
              data
            })
        }else{
          alert(data.msg)
        }
    });
  }
}

export function  loadServenawardDetail(gameCode,pageNo,cb=()=>{}) {
  return (dispatch,getState) =>{
    post(AWARD.loadawardDetailAPI(),{
      gameCode,
      pageNo
    })
    .then(res => res.json())
    .then(data => {
        dispatch({
          type: actionType.REQUEST_SERVEN_AWARD_DETAIL,
          data: {
            type:gameCode,
            data
          }
        })
        cb()
    });
  }
}

export function tempSaveSelectedPeiyu(selectedPeiyu) {
  return {
    type: actionType.TEMP_SAVE_SELECTED_PEIYU,
    data: selectedPeiyu
  };
}

export function tempSaveSelectedNameAndMoney(name,money) {
  return {
    type: actionType.TEMP_SAVE_SELECTED_NAME_AND_MONEY,
    data: {
      name,
      money
    }
  };
}

export function getMultiGroupOrder(gameCode, nums, multilen, cb = (ok,data) => {}) {
  return (dispatch, getState) => {
    post(API.getMultiGroupOrderAPI(), {
      gameCode,
      cids: nums.join(','),
      multilen
    })
    .then(res => res.json())
    .then(data => {
      dispatch(multiGroupOrder(data));
      if(data.errorCode == RES_OK_CODE){
        cb(1,data);
      }else{
        cb(0,data)
      }
    });
  };
}

export function multiGroupOrder(data) {
  return {
    type: actionType.REQUEST_MULTI_GROUP_ORDER,
    data
  };
}

export function getGroupOrder(gameCode, itemCode, xzlxCode, multilen, nums, cb = (ok,data) => {}) {
  return (dispatch, getState) => {
    post(API.getGroupOrderAPI(), {
      gameCode,
      itemCode,
      xzlxCode,
      multilen,
      number: nums.join(','),
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: actionType.REQUEST_MULTI_GROUP_ORDER,
        data
      });
      if(data.errorCode == RES_OK_CODE){
        cb(1,data);
      }else{
        cb(0,data)
      }
    });
  };
}

export function getLmOrder(gameCode, nums, rrtype, pabc, cb = () => {},  dm1 = 0, dm2 = 0) {
  return (dispatch, getState) => {
    post(API.getLMOrderAPI(), {
      gameCode,
      nums,
      rrtype,
      pabc,
      dm1,
      dm2
    })
    .then(res => res.json())
    .then(data => {
      dispatch(preOrderItems(data));
      cb(data);
    });
  };
}

export function preOrderItems(data) {
  return {
    type: actionType.REQUEST_PREORDER_ITEMS,
    data
  }; 
}

export function saveOrder(gameCode, jsons, cb= () => {}, orderFlag = 'normal') {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(API.saveOrderAPI(), {
      gameCode,
      userName,
      orderFlag,
      jsons: JSON.stringify(jsons)
    })
    .then(res => res.json())
    .then(data => {
      cb(data);
    });
  };
}


export function changeLMLeibie(data) {
  return {
    type: actionType.CHANGE_LM_LEIBIE,
    data
  }
}

export function changeLMPlayModule(data) {
  return {
    type: actionType.CHANGE_LM_PLAYMODULE,
    data
  }
}

export function autoSelectLM(data) {
  return {
    type:actionType.AUTO_SELECT_LM,
    data
  }
}

export function LMReset(data) {
  return {
    type:actionType.LM_RESET,
    data
  }
}

export function removeAllselect() {
  return {
    type:actionType.REMOVE_ALL_SELECT,
    data:0
  }
}